package cn.ycl.system.other.study.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YT-007
 */
public class BinarySearch {

    public static void main(String[] args) {
        int[] arr = {1,4,7,9,9,9,12,18,22,23,24,25,26,27,28,29,30,31,32,45};
        int target = 9;
        int bs = bs(arr, 0, arr.length - 1, target);
        boolean flag1 = true,flag2 = true;
        int temp1 = bs - 1,temp2 = bs + 1;
        while(flag1 || flag2){
            if(flag1){
                if(temp1 >= 0 && arr[temp1] == target){
                    System.out.println(temp1);
                    temp1--;
                }else{
                    flag1 = false;
                }
            }
            if(flag2){
                if(temp2 < arr.length && arr[temp2] == target){
                    System.out.println(temp2);
                    temp2++;
                }else{
                    flag2 = false;
                }
            }
        }
        System.out.println(bs);
    }
    public static Integer bs(int[] arr,int left,int right,int target){
        int mid = (left + right)/2;
        if(arr[mid] == target){
            return mid;
        }
        if(arr[mid] < target){
            return bs(arr,mid + 1,right,target);
        }else{
            return bs(arr,left,mid - 1,target);
        }
    }
}
